<?php
class picture extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('modalbum');
		$this->load->model('modpicture');
	}
	
	function index($id_album)
	{
		$token = $this->session->userdata('token');
		if($token){
			$data['album'] = $this->modalbum->detail($id_album);			$page = 100;
			$data['results'] = $this->modpicture->getList($id_album,$page);
			$this->load->view('picture/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add($id_album){
		$data['mode'] = "Tambah Picture";
		$data['action'] = "save";
		$data['album'] = $id_album;
		$this->load->view('picture/form',$data);
	}
	
	function edit($id_picture,$id_album){
		$data['mode'] = "Ubah Picture";
		$data['action'] = "update";
		$data['album'] = $id_album;
		$data['detail'] = $this->modpicture->detail($id_picture);
		$this->load->view('picture/form',$data);
	}
	
	function save()
	{
		$id_picture = $this->input->post('id_picture');
		$id_album = $this->input->post('id_album');
		$tgl_picture = date('Y-m-d');
		$keterangan = $this->input->post('keterangan');
		
		$config['upload_path'] = './images/uploads/content_images';
		$config['allowed_types'] = 'jpg|png|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('nama_picture');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$nama_picture="";
			}
			else{
				$nama_picture = $getFile['file_name'];
			}
		}else{
			$nama_picture = $this->input->post('nama_picture_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$this->modpicture->setData($id_picture,$id_album,$nama_picture,$tgl_picture,$keterangan);
			$this->modpicture->create();
			$this->session->set_flashdata('success', true);
			redirect('picture/index/'.$id_album);
		}
		$this->session->set_flashdata('error', true);
		redirect('picture/index/'.$id_album);
	}
	
	function update()
	{
		$id_picture = $this->input->post('id_picture');
		$id_album = $this->input->post('id_album');
		$tgl_picture = $this->input->post('tgl_picture');
		$keterangan = $this->input->post('keterangan');
		
		$config['upload_path'] = './images/uploads/content_images';
		$config['allowed_types'] = 'jpg|png|gif';
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('nama_picture');
		$getFile = $this->upload->data();
		$path_foto = $getFile['file_name'];
		
		if($path_foto != ""){	
			if ($this->upload->display_errors())
			{
				$this->session->set_flashdata('error', true);
				$nama_picture="";
			}
			else{
				$nama_picture = $getFile['file_name'];
			}
		}else{
			$nama_picture = $this->input->post('nama_picture_tmp');
		}
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->modpicture->setData($id_picture,$id_album,$nama_picture,$tgl_picture,$keterangan);
			$this->modpicture->update($id_picture);
			$this->session->set_flashdata('success', true);
			redirect('picture/index/'.$id_album);
		}
		$this->session->set_flashdata('error', true);
		redirect('picture/index/'.$id_album);
	}
	
	function delete($id_picture,$id_album)
	{
		$getHapus = $this->modpicture->detail($id_picture);
		@unlink("images/uploads/content_images/".$getHapus[0]['nama_picture']);
		
		if ($this->modpicture->remove($id_picture)){
			$this->session->set_flashdata('delete', true);
			redirect('picture/index/'.$id_album,'refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('picture/index/'.$id_album);
	}	
	
	function searchData(){
		$name = $this->input->post('name');
		$data['results'] = $this->modpicture->getListSearch($name);
		$this->load->view('picture/list',$data);
	}
}
?>